package com.think.common.enums.serialize;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.think.common.cons.SysCons;
import com.think.common.enums.IEnum;

import java.io.IOException;

/**
 * <p>
 * jackson IEnum 序列化器
 * </p>
 *
 * @author xwj
 * @date 2019/4/11
 */
public class IEnumSerialize extends StdSerializer<IEnum> {

    public IEnumSerialize(Class<IEnum> i) {
        super(i);
    }

    @Override
    public void serialize(IEnum iEnum, JsonGenerator gen, SerializerProvider provider) throws IOException {
        int code = iEnum.getValue();
        gen.writeNumber(code);
        gen.writeStringField(gen.getOutputContext().getCurrentName() + SysCons.ENUM_SERIALIZE_POSTFIX,
                iEnum.getMsg());
    }
}
